package com.web.work02_27Servlet;

import com.web.JDBCUtils;
import com.web.servlet.HelloServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/insertSt")
public class InsertStudent extends HelloServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置内容类型
        resp.setContentType("application/json;charset=UTF-8");
        //接受信息
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String address = req.getParameter("address");
        String idcard = req.getParameter("idcard");
        //添加学生信息
        if (name != null && age != null && gender != null && address != null && idcard != null){
            //当信息完全时，可以添加数据
            if (JDBCUtils.update("insert into student(name,age,gender,address,idcard) values(?,?,?,?,?)", name, Integer.parseInt(age), gender, address, idcard) == 1){
                //添加成功
                resp.getWriter().write("msg:添加成功");
            }else {
                //添加失败
                resp.getWriter().write("msg:添加失败");
            }
        }else {
            //信息不完全，不能添加数据
            resp.getWriter().write("msg:信息不完整");
        }
    }
}
